<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="供应商名称" v-model="supplierinfo.name"> </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item"  v-if="btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
    </div>
    <!--<ul v-for="(article, index) in articles" :key="index" class="caption" >-->
    <el-table :key='uniqueKey' :data="supplierlist" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
      <!--<el-table-column align="center" label="序号" width="65"> <template scope="scope">-->
        <!--<span>{{scope.row.id}}</span>-->
      <!--</template> </el-table-column>-->
      <el-table-column width="200" align="center" label="供应商名称"> <template scope="scope">
        <span>{{scope.row.name}}</span>
      </template> </el-table-column>
      <el-table-column width="110" align="center" label="联系人"> <template scope="scope">
        <span>{{scope.row.contacts}}</span>
      </template> </el-table-column>
      <el-table-column width="110" align="center" label="联系电话"> <template scope="scope">
        <span>{{scope.row.phone}}</span>
      </template> </el-table-column>
      <el-table-column width="300" align="center" label="电子邮箱"> <template scope="scope">
        <span>{{scope.row.email}}</span>
      </template> </el-table-column>
      <el-table-column width="180" align="center" label="微信"> <template scope="scope">
        <span>{{scope.row.wechat}}</span>
      </template> </el-table-column>
      <el-table-column width="200" align="center" label="地址"> <template scope="scope">
        <span>{{scope.row.address}}</span>
      </template> </el-table-column>
      <el-table-column align="center" label="操作" width="150"> <template scope="scope">
        <el-button v-if="btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button v-if="btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
        </el-button>
      </template> </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
<!--        prop 校验字段的属性名字-->
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入供应商名称"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱"></el-input>
        </el-form-item>
        <el-form-item label="微信" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { mapGetters } from 'vuex';
  import * as supplierService from 'api/business/supplier';

  export default {
    name: 'supplierinfo',
    data() {
      return {
        btn_add: false,
        btn_edit: false,
        btn_del: false,
        textMap: {
          update: '修改',
          create: '新增'
        },
        dialogStatus: '', // 对弹出层是创建还是修改
        dialogFormVisible: false, // 弹出层的显示状态
        rules: {
          name: [
            {
              required: true,
              message: '请输入用户',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 80,
              message: '长度在 3 到 80 个字符',
              trigger: 'blur'
            }
          ],
          username: [
            {
              required: true,
              message: '请输入账户',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 80,
              message: '长度在 3 到 80 个字符',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 5,
              max: 80,
              message: '长度在 5 到 20 个字符',
              trigger: 'blur'
            }
          ]
        },
        supplierlist: null,
        form: {
          id: '',
          name: '',
          contacts: '',
          phone: '',
          email: '',
          wechat: '',
          description: '',
          enabled: '',
          fax: '',
          address: '',
          taxNum: '',
          bankName: '',
          accountNumber: '',
          taxRate: '',
          del_flag: ''
        },
        supplierinfo: {
          id: '',
          name: '',
          contacts: '',
          phone: '',
          email: '',
          wechat: '',
          description: '',
          enabled: '',
          fax: '',
          address: '',
          taxNum: '',
          bankName: '',
          accountNumber: '',
          taxRate: '',
          del_flag: ''
        },
        uniqueKey: 0,
        listLoading: false,
        supplierinfo_btn_add: false,
        supplierinfo_btn_edit: false,
        supplierinfo_btn_del: false,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        total: null
      }
    },
    computed: {
      // 此处需要...扩展
      // 替代 this.$store.getter.elements
      ...mapGetters([
        'elements'
      ])
    },
    created() {
      this.getList();
      console.info(this.elements);
      this.btn_add = this.elements['supplierManager:btn_add'];
      this.btn_edit = this.elements['supplierManager:btn_edit'];
      this.btn_del = this.elements['supplierManager:btn_del'];
    },
    methods: {
      getList() {
        console.info('初始化数据列表');
        supplierService.getAll().then(result => {
          console.info(result);
          this.supplierlist = result;
        })
      },
      findByName() {
        console.info(this.supplierinfo.name);
        supplierService.findByName(this.supplierinfo).then(result => {
          console.info(result);
          if (result != undefined && result != null && result.status == 200) {
            this.supplierlist = result.data;
          }
          // this.supplierlist = result;
        })
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false;
            supplierService.updateOne(this.form.id, this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },

      handleFilter() {
        this.findByName();
      },
      resetForm() {
        this.form = {
          name: '',
          contacts: '',
          phone: '',
          email: '',
          wechat: '',
          address: ''
        };
      },
      create(form) {
        const set = this.$refs;
        set[form].validate(valid => {
          if (valid) {
            console.info(this.form);
            supplierService.addOne(this.form)
              .then(() => {
                this.dialogFormVisible = false;
                this.getList();
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                });
              })
          } else {
            return false;
          }
        });
      },
      cancel(formName) {
        this.dialogFormVisible = false;
        this.$refs[formName].resetFields();
      },

      handleCreate() {
        this.resetForm();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },

      handleUpdate(row) {
        console.info(row);
        supplierService.getOne(row.id)
            .then(response => {
              this.form = response.data;
              this.dialogFormVisible = true;
              this.dialogStatus = 'update';
            });
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            supplierService.deleteOne(row.id)
              .then(response => {
                console.info(response);
                // 默认提示成功,可以根据response.status ==200 response.data==1 判断删除成功与否
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                });
                const index = this.supplierlist.indexOf(row);
                this.supplierlist.splice(index, 1);
              });
          });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      }
    }
  }
</script>

<style scoped>

</style>
